from django.core.management.base import BaseCommand
from web.models import Transaction, UserInfo

class Command(BaseCommand):
    help = 'Fix invalid foreign key data in web_transaction'

    def handle(self, *args, **options):
        # 删除无效的 web_transaction 记录
        invalid_transactions = Transaction.objects.filter(user_id__isnull=False).exclude(user_id__in=UserInfo.objects.values_list('id', flat=True))
        count = invalid_transactions.count()
        invalid_transactions.delete()
        self.stdout.write(self.style.SUCCESS(f'Successfully deleted {count} invalid transactions'))